﻿using Microsoft.CodeAnalysis;

namespace AutoRevit.Generator;

internal class ReportDiagnostic
{
    internal readonly static DiagnosticDescriptor IsStaticDescriptor = new(
                        id: "ARG001",
                        title: "Invalid Method Symbol",
                        messageFormat: "The method '{0}' does not meet the required conditions for code generation. It needs to be static.",
                        category: "AutoRevit.Generator",
                        DiagnosticSeverity.Error,
                        isEnabledByDefault: true);

    internal readonly static DiagnosticDescriptor ReturnsVoidDescriptor = new (
                        id: "ARG002",
                        title: "Invalid Method Symbol",
                        messageFormat: "The method '{0}' does not meet the required conditions for code generation. Non void type.",
                        category: "AutoRevit.Generator",
                        DiagnosticSeverity.Error,
                        isEnabledByDefault: true);

    internal readonly static DiagnosticDescriptor NotReturnsVoidDescriptor = new (
                        id: "ARG003",
                        title: "Invalid Method Symbol",
                        messageFormat: "The method '{0}' does not meet the required conditions for code generation. It cannot be void.",
                        category: "AutoRevit.Generator",
                        DiagnosticSeverity.Error,
                        isEnabledByDefault: true);

    internal readonly static DiagnosticDescriptor IsPublicOrInternalDescriptor = new (
                        id: "ARG004",
                        title: "Invalid Method Symbol",
                        messageFormat: "The method '{0}' does not meet the required conditions for code generation. The access modifier must be either Public or Internal.",
                        category: "AutoRevit.Generator",
                        DiagnosticSeverity.Error,
                        isEnabledByDefault: true);

    internal static bool ValidIsStatic(GeneratorExecutionContext context, IMethodSymbol methodSymbol)
    {
        if (methodSymbol.IsStatic)
        {
            return true;
        }

        context.ReportDiagnostic(Diagnostic.Create(IsStaticDescriptor, methodSymbol.Locations.FirstOrDefault(), methodSymbol.Name));

        return false;
    }

    internal static bool ValidReturnsVoid(GeneratorExecutionContext context, IMethodSymbol methodSymbol)
    {
        if (methodSymbol.ReturnsVoid)
        {
            return true;
        }

        context.ReportDiagnostic(Diagnostic.Create(
                    ReturnsVoidDescriptor,
                    methodSymbol.Locations.FirstOrDefault(),
                    methodSymbol.Name));

        return false;
    }

    internal static bool ValidNotReturnsVoid(GeneratorExecutionContext context, IMethodSymbol methodSymbol)
    {
        if (!methodSymbol.ReturnsVoid)
        {
            return true;
        }

        context.ReportDiagnostic(Diagnostic.Create(
                    NotReturnsVoidDescriptor,
                    methodSymbol.Locations.FirstOrDefault(),
                    methodSymbol.Name));

        return false;
    }

    internal static bool ValidIsPublicOrInternal(GeneratorExecutionContext context, IMethodSymbol methodSymbol)
    {
        // 检查方法是否是 public
        if (methodSymbol.DeclaredAccessibility == Accessibility.Public)
        {
            return true;
        }

        // 检查方法是否是 internal
        if (methodSymbol.DeclaredAccessibility == Accessibility.Internal)
        {
            return true;
        }

        context.ReportDiagnostic(Diagnostic.Create(
                    IsPublicOrInternalDescriptor,
                    methodSymbol.Locations.FirstOrDefault(),
                    methodSymbol.Name));

        return false;
    }
}
